<?php
/**
 * Created by PhpStorm.
 * User: chouxiaoya
 * Date: 2016/7/5
 * Time: 14:48
 */

namespace Admin\Controller;

class MasterController extends CommonController{

    protected function _initialize(){
        parent::_initialize();
        $this->breadcrumb1 = '师傅管理';
        $this->url = U('Master/index');
    }

    public function index(){
        $name   = I('get.name');
        $phone  = I('get.phone');
        $status = I('get.status','all');
        $start_time = I('get.start_time');
        $end_time = I('get.end_time');
        $start_time = strtotime($start_time);
        $end_time = strtotime($end_time);
        $map['usr.is_real'] = 1;
        if($name){
            $map['zsf_user_real_info.id_name'] = array('like',"%$name%");
        }
        if($phone){
            $map['usr.phone'] = array('like',"%$phone%");
        }
        if($status != 'all'){
            $map['usr.master_status'] = $status;
            if($status == 1 || $status == 2){
                $map['usr.master_status'] = array('in',"$status,3");
            }
        }
        if($start_time && $end_time) {
            $map['usr.add_time'] = array('between',$start_time.','.$end_time);
        }elseif($start_time){
            $map['usr.add_time'] = array('egt',$start_time);
        }
        $order_count = M('repair_order')->where('status !=-1')->count();
        $order_total = M('repair_order')->field('SUM(total) as total')->where('status !=-1')->find();

        $count = M()->table('zsf_user usr')
            ->field('usr.user_id,usr.user_status,usr.master_status,usr.user_face,usr.user_name,usr.phone,usr.invite,usr.add_time,addr.address,addr.status,zsf_user_real_info.id_name,zsf_user_real_info.id_num')
            ->join('LEFT JOIN zsf_address addr on usr.user_id = addr.user_id and addr.status = 1 and addr.is_del !=1')
            ->join('zsf_user_real_info on zsf_user_real_info.user_id = usr.user_id','left')
            ->where($map)
            ->count();
        $Page  = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
        $show  = $Page->show();// 分页显示输出
        $data = M()->table('zsf_user usr')
                ->field('usr.user_id,usr.user_status,usr.master_status,usr.user_face,usr.user_name,usr.phone,usr.invite,usr.add_time,addr.address,addr.status,zsf_user_real_info.id_name,zsf_user_real_info.id_num')
                ->join('LEFT JOIN zsf_address addr on usr.user_id = addr.user_id and addr.status = 1 and addr.is_del !=1')
                ->join('zsf_user_real_info on zsf_user_real_info.user_id = usr.user_id','left')
                ->where($map)
                ->order(array('usr.user_id'=>'asc'))
                ->limit($Page->firstRow.','.$Page->listRows)
                ->select();
        foreach($data as $k=>$v){
            $data[$k]['add_time'] = date('Y-m-d H:i:s',$data[$k]['add_time']);
            $data[$k]['user_face'] = $data[$k]['user_face'].'?imageView2/1/w/75/h/65';
        }
        $this->breadcrumb2='已审核师傅';
        $this->assign('count',$order_count);
        $this->assign('total',$order_total['total']);
        $this->assign('user',$data);
        $this->assign('page',$show);
        $this->display();
    }

    //待审核师傅
    public function watingAudit()
    {
        $name   = I('get.name');
        $phone  = I('get.phone');
        $num    = I('get.num');
        $start_time = I('get.start_time');
        $end_time = I('get.end_time');
        $start_time = strtotime($start_time);
        $end_time = strtotime($end_time);
        $map['usr.is_real'] = 2;
        if($name){
            $map['zsf_user_real_info.id_name'] = array('like',"%$name%");
        }
        if($phone){
            $map['usr.phone'] = array('like',"%$phone%");
        }
        if($num){
            $map['zsf_user_real_info.id_num'] = array('like',"%$num%");
        }
        if($start_time && $end_time) {
            $map['zsf_user_real_info.add_time'] = array('between',$start_time.','.$end_time);
        }elseif($start_time){
            $map['zsf_user_real_info.add_time'] = array('egt',$start_time);
        }
        $count = M()->table('zsf_user usr')
            ->field('usr.user_id,usr.user_status,usr.master_status,usr.user_face,
            usr.user_name,usr.phone,usr.invite,zsf_user_real_info.id_name,
            zsf_user_real_info.id_num,zsf_user_real_info.pic_psitive,zsf_user_real_info.pic_other_side,zsf_user_real_info.pic_hold,zsf_user_real_info.add_time')
            ->join('zsf_user_real_info on zsf_user_real_info.user_id = usr.user_id','left')
            ->where($map)
            ->count();
        //var_dump($count);exit;
        $Page  = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
        $show  = $Page->show();// 分页显示输出

        $data = M()->table('zsf_user usr')
            ->field('usr.user_id,usr.user_status,usr.master_status,usr.user_face,
            usr.user_name,usr.phone,usr.invite,zsf_user_real_info.id_name,
            zsf_user_real_info.id_num,zsf_user_real_info.pic_psitive,zsf_user_real_info.pic_other_side,zsf_user_real_info.pic_hold,zsf_user_real_info.add_time')
            ->join('zsf_user_real_info on zsf_user_real_info.user_id = usr.user_id','left')
            ->where($map)
            ->order(array('usr.user_id'=>'asc'))
            ->limit($Page->firstRow.','.$Page->listRows)
            ->select();
        //echo '<pre>';
        //var_dump($data);exit;
        foreach($data as $k=>$v){
            $data[$k]['add_time'] = date('Y-m-d H:i:s',$data[$k]['add_time']);
        }
        $this->breadcrumb2='待审核师傅';
        $this->assign('user',$data);
        $this->assign('page',$show);
        $this->display();
    }

    //通过不通过
    public function passStatus()
    {
        $user_id  = I('post.user_id');
        $status   = I('post.status');
        $userInfo = M('user')->field('is_real,phone')->where("user_id = $user_id")->find();
        $realInfo = M('user_real_info')->where("user_id = $user_id");
        $a = session('user_auth');
        $real_data['user_id'] = $user_id;
        $real_data['a_id'] = $a['uid'];
        $real_data['audit_time'] = time();
        if($userInfo && $userInfo['is_real'] == 2){
            if($status == 1){
                $data['is_real'] = 1;
                $data['role'] = 2;
                //审核通过，生成邀请码
                //$str = $this->createNonceStr();
                //$data['invite'] = $userInfo['phone'];
               $money_data['user_id']=$user_id;
                M('user_money')->add($money_data);
            }else{
                $data['is_real'] = 3;
            }
        }
        $data['user_id'] = $user_id;
        $info = M('user')->save($data);
        $a = M('user_real_info')->save($real_data);
        if($info){
            echo 1;
        }else{
            echo 2;
        }
    }

    //通过不通过
    public function why()
    {
        if(IS_GET){
            $user_id  = I('get.user_id');
            $a = session('user_auth');
            $admin =  M('admin')->field('a_true_name')->where("a_id = {$a['uid']}")->find();
            $this->breadcrumb2='不通过原因';
            $this->assign('admin',$admin);
            $this->assign('userid',$user_id);
            $this->display();
        }
        if(IS_POST){
            $user_id  = I('post.user_id');
            $why      = I('post.why');
            $userInfo = M('user')->field('is_real')->where("user_id = $user_id")->find();
            $realInfo = M('user_real_info')->where("user_id = $user_id");
            $a = session('user_auth');
            $real_data['user_id'] = $user_id;
            $real_data['a_id'] = $a['uid'];
            $real_data['why_nopass'] = $why;
            $real_data['audit_time'] = time();
            if($userInfo && $userInfo['is_real'] == 2){
                $data['user_id'] = $user_id;
                $data['is_real'] = 3;
                M('user')->save($data);
                M('user_real_info')->save($real_data);
            }
            redirect('admin.php?s=Master/watingAudit');
        }
    }
    private function createNonceStr($length = 16) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }
    //待审核师傅
    public function noPass()
    {
        $name   = I('get.name');
        $phone  = I('get.phone');
        $num    = I('get.num');
        $start_time = I('get.start_time');
        $end_time = I('get.end_time');
        $start_time = strtotime($start_time);
        $end_time = strtotime($end_time);
        $map['usr.is_real'] = 3;
        if($name){
            $map['zsf_user_real_info.id_name'] = array('like',"%$name%");
        }
        if($phone){
            $map['usr.phone'] = array('like',"%$phone%");
        }
        if($num){
            $map['zsf_user_real_info.id_num'] = array('like',"%$num%");
        }
        if($start_time && $end_time) {
            $map['zsf_user_real_info.add_time'] = array('between',$start_time.','.$end_time);
        }elseif($start_time){
            $map['zsf_user_real_info.add_time'] = array('egt',$start_time);
        }
        $count = M()->table('zsf_user usr')
            ->field('usr.user_id,usr.user_status,usr.master_status,usr.user_face,
            usr.user_name,usr.phone,usr.invite,zsf_user_real_info.id_name,
            zsf_user_real_info.id_num,zsf_user_real_info.pic_psitive,
            zsf_user_real_info.pic_other_side,zsf_user_real_info.pic_hold,
            zsf_user_real_info.add_time,zsf_user_real_info.audit_time,zsf_user_real_info,a_id,zsf_user_real_info.why_nopass')
            ->join('zsf_user_real_info on zsf_user_real_info.user_id = usr.user_id','left')
            ->where($map)
            ->count();
        //var_dump($count);exit;
        $Page  = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
        $show  = $Page->show();// 分页显示输出

        $data = M()->table('zsf_user usr')
            ->field('usr.user_id,usr.user_status,usr.master_status,usr.user_face,
            usr.user_name,usr.phone,usr.invite,zsf_user_real_info.id_name,
            zsf_user_real_info.id_num,zsf_user_real_info.pic_psitive,
            zsf_user_real_info.pic_other_side,zsf_user_real_info.pic_hold,
            zsf_user_real_info.add_time,zsf_user_real_info.audit_time,zsf_user_real_info.a_id,
            zsf_user_real_info.why_nopass,zsf_admin.a_true_name')
            ->join('zsf_user_real_info on zsf_user_real_info.user_id = usr.user_id','left')
            ->join('zsf_admin on zsf_user_real_info.a_id = zsf_admin.a_id','left')
            ->where($map)
            ->order(array('usr.user_id'=>'asc'))
            ->limit($Page->firstRow.','.$Page->listRows)
            ->select();
        //echo '<pre>';
        //var_dump($data);exit;
        foreach($data as $k=>$v){
            $data[$k]['add_time'] = date('Y-m-d H:i:s',$data[$k]['add_time']);
            $data[$k]['audit_time'] = date('Y-m-d H:i:s',$data[$k]['audit_time']);
        }
        $this->breadcrumb2='未通过审核师傅';
        $this->assign('user',$data);
        $this->assign('page',$show);
        $this->display();
    }

    //银行卡管理
    public function bank()
    {
        $name   = I('get.name');
        $phone  = I('get.phone');
        $num    = I('get.num');
        $map['usr.is_real'] = 1;//只查询审核通过师傅银行卡信息
        if($name){
            $map['zsf_user_real_info.id_name'] = array('like',"%$name%");
        }
        if($phone){
            $map['usr.phone'] = array('like',"%$phone%");
        }
        if($num){
            $map['zsf_user_real_info.id_num'] = array('like',"%$num%");
        }
        $count = M()->table('zsf_user usr')
            ->field('usr.user_id,usr.phone,usr.user_face,zsf_user_real_info.id_name,zsf_user_real_info.id_num,zsf_user_real_info.add_time')
            ->join('zsf_user_real_info on zsf_user_real_info.user_id = usr.user_id','left')
            ->join('zsf_bank_card on zsf_bank_card.id_num = zsf_user_real_info.id_num','left')
            ->where($map)
            ->count();
        $Page  = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
        $show  = $Page->show();// 分页显示输出
        $data = M()->table('zsf_user usr')
            ->field('usr.user_id,usr.phone,usr.user_face,zsf_user_real_info.id_name,zsf_user_real_info.id_num,zsf_user_real_info.add_time,
            IFNULL((select count(*) from zsf_bank_card where id_num = zsf_user_real_info.id_num group by id_num),0) as num')
            ->join('zsf_user_real_info on zsf_user_real_info.user_id = usr.user_id','left')
            ->join('zsf_bank_card on zsf_user_real_info.id_num = zsf_bank_card.id_num','left')
            ->where($map)
            ->group('usr.user_id')
            ->order(array('usr.user_id'=>'asc'))
            ->limit($Page->firstRow.','.$Page->listRows)
            ->select();
        foreach($data as $k=>$v){
            $data[$k]['add_time'] = date('Y-m-d H:i:s',$data[$k]['add_time']);
        }
        $this->breadcrumb2='师傅银行卡管理';
        $this->assign('user',$data);
        $this->assign('page',$show);
        $this->display();
    }

    //银行卡详情
    public function bankDetail()
    {
        $num    = I('get.num');
        if($num){
            $map['a.id_num'] = $num;
        }
        $count = M()->table('zsf_bank_card a')
            ->field('a.bank_num,b.bank_name')
            ->join('LEFT JOIN zsf_bank_list b on a.bank_id = b.bank_id')
            ->where($map)
            ->count();
        $Page  = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
        $show  = $Page->show();// 分页显示输出
        $data =  M()->table('zsf_bank_card a')
            ->field('a.bank_num,b.bank_name')
            ->join('LEFT JOIN zsf_bank_list b on a.bank_id = b.bank_id')
            ->where($map)
            ->order(array('a.id'=>'asc'))
            ->limit($Page->firstRow.','.$Page->listRows)
            ->select();
        $this->breadcrumb2='银行卡明细';
        $this->assign('user',$data);
        $this->assign('page',$show);
        $this->display();
    }

    //服务配置
    public function serverConfig()
    {
        $this->breadcrumb2='服务配置详情';
        $allCats = M('category')->select();
        $allCats = $this->noLimit($allCats,0);
        //echo '<pre>';
        //dump($allCats);
        $user_id = I('get.user_id');
        $user_cats    = M('user_repair_conf')->where("user_id = $user_id")->select();
        $user_normal  = array();
        $user_major   = array();
        if($user_cats){
            foreach($user_cats as $k=>$v){
                if($v['is_good'] == 0){
                    $user_normal[] = $v;
                }else{
                    $user_major[]  = $v;
                }
            }
        }
        $all_normal = $all_major = $allCats;
        foreach($all_normal as $k=>$v){
            $all_normal[$k]['selected'] = 0;
            foreach($user_normal as $k1=>$v1){
                if($v['category_id'] == $v1['category_id']){
                    $all_normal[$k]['selected'] = 1;
                }
            }
        }
        foreach($all_major as $k=>$v){
            $all_major[$k]['selected'] = 0;
            foreach($user_major as $k1=>$v1){
                if($v['category_id'] == $v1['category_id']){
                    $all_major[$k]['selected'] = 1;
                }
            }
        }
        $this->assign('normal',$all_normal);
        $this->assign('major',$all_major);
        $this->display();
    }
    private function noLimit($allCats,$pid,$level=0)
    {
       static $list = array();
        foreach($allCats as $k=>$v){
            if($v['pid'] == $pid){
                $v['level'] = $level;
                $list[] = $v;
                $this->noLimit($allCats,$v['category_id'],$level+1);
            }
        }
        return $list;
    }
    //开启关闭邀请码功能
    public function invite()
    {
        $user_id  = I('post.user_id');
        $status   = I('post.status');
        $userInfo = M('user')->field('phone,invite')->where("user_id = $user_id")->find();
        if($userInfo){
            if($status == 1){
                //生成邀请码
                $data['invite'] = $userInfo['phone'];
            }else{
                $data['invite'] = '';
            }
        }
        $data['user_id'] = $user_id;
        $info = M('user')->save($data);
        if($info){
            echo 1;
        }else{
            echo 2;
        }
    }
}